-- 结束一切吧技能
local LuaJieshu = fk.CreateSkill {
    name = "LuaJieshu",
    anim_type = "drawcard",
}

Fk:loadTranslationTable{
    ["LuaJieshu"] = "结束一切吧",
    ["#LuaJieshu-trigger"] = "结束一切吧：一名角色已发动技能超过四次",
    ["#LuaJieshu-option1"] = "①摸三张牌，然后弃置2张牌，对一名角色造成1点伤害",
    ["#LuaJieshu-option2"] = "②摸两张牌，然后立即使用一张牌，此牌不可响应",
    ["#LuaJieshu-option3"] = "③摸一张牌，令一名角色执行一次弃牌阶段",
    ["#LuaJieshu-choose_targe1"] = "结束一切吧：请选择一名目标角色造成1点伤害。",
    ["#LuaJieshu-choose_targe2"] = "结束一切吧：请选择一名目标角色其执行弃牌阶段。",
    ["#LuaJieshu-no_response"] = "此牌不可响应",
    ["@jieshu_triggered-turn"] = "结束一切",
    ["#LuaJieshu-response"] = "结束一切吧：请选择一张牌，你使用此牌且此牌不可响应。",
    [":LuaJieshu"] = "一个回合内，一名其他角色发动武将牌上的技能超过四次，你选择X项执行，然后立即结束当前回合：①摸三张牌，然后弃置2张牌，对一名角色造成1点伤害。②摸两张牌，然后立即使用一张牌，此牌不可响应。③摸一张牌，令一名角色执行一次弃牌阶段。（X为已损体力值）",
}

-- 记录每回合技能使用次数
LuaJieshu:addEffect(fk.AfterSkillEffect, {
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(LuaJieshu.name) and target and target ~= player and
        table.contains(target:getSkillNameList(), data.skill:getSkeleton().name) and not data.skill.is_delay_effect
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = player.room
        room:addPlayerMark(target, "@jieshu_triggered-turn", 1)
        
        -- 检查是否有角色使用技能超过4次
        if target:getMark("@jieshu_triggered-turn") >= 4 then
            room:setPlayerMark(target, "@jieshu_triggered-turn", 0)
            if player:getLostHp() > 0 then
                 -- 选择执行哪个选项
                local choices = {
                    "#LuaJieshu-option1", 
                    "#LuaJieshu-option2", 
                    "#LuaJieshu-option3"
                }
                local choice = player.room:askToChoices(player, {
                    choices = choices,
                    min_num = 1,
                    max_num = player:getLostHp(),
                    skill_name = LuaJieshu.name,
                    prompt = "#lvzheng-select",
                    cancelable = false,
                })

                if table.contains(choice, "#LuaJieshu-option1") then
                    -- 选项1：摸三张牌，弃置2张牌，造成伤害
                    player:drawCards(3, LuaJieshu.name)
                    
                    -- 弃置2张牌
                    if not player:isKongcheng() then
                        room:askToDiscard(player, {
                            min_num = 2,
                            max_num = 2,
                            include_equip = true,
                            skill_name = LuaJieshu.name,
                            cancelable = false,
                        })
                    end
                    
                    -- 选择一名角色造成伤害

                    local targets = room:askToChoosePlayers(player, {
                        targets = room:getAlivePlayers(),
                        min_num = 1,
                        max_num = 1,
                        prompt = "#LuaJieshu-choose_targe1",
                        skill_name = LuaJieshu.name,
                        cancelable = false
                    })
                    if #targets > 0 then
                        local target = targets[1]
                        room:damage({
                            from = player,
                            to = target,
                            damage = 1,
                            skillName = LuaJieshu.name
                        })
                    end
                end
                if table.contains(choice, "#LuaJieshu-option2") then
                    -- 选项2：摸两张牌，使用一张不可响应的牌
                    player:drawCards(2, LuaJieshu.name)
                    
                    -- 选择使用一张牌
                    if not player:isKongcheng() then
                        local use = player.room:askToUseCard(player, {
                            skill_name = LuaJieshu.name,
                            pattern = ".",
                            cancelable = true,
                            prompt = "#LuaJieshu-response",
                        })
                        if use then
                            room:setPlayerMark(player, "bkxy", 1)
                            room:useCard(use)
                        end
                        
                    end
                end
                if table.contains(choice, "#LuaJieshu-option3") then
                    -- 选项3：摸一张牌，令一名角色执行弃牌阶段
                    player:drawCards(1, LuaJieshu.name)
                    
                    local targets = room:askToChoosePlayers(player, {
                        targets = room:getAlivePlayers(),
                        min_num = 1,
                        max_num = 1,
                        prompt = "#LuaJieshu-choose_targe2",
                        skill_name = LuaJieshu.name,
                        cancelable = false
                    })
                    if #targets > 0 then
                        local target = targets[1]
                        target:gainAnExtraPhase(Player.Discard,LuaJieshu.name,false)
                    end
                end
                -- 立即结束当前回合
                room:endTurn()
            end
        end
    end,
})

LuaJieshu:addEffect(fk.PreCardUse, {
    is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(LuaJieshu.name) and player:getMark("bkxy") == 1
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "bkxy", 0)
    data.disresponsiveList = data.disresponsiveList or {}
    for _, p in ipairs(room:getAlivePlayers()) do
      if not table.contains(data.disresponsiveList, p) then
        table.insert(data.disresponsiveList, p)
      end
    end
  end
})

  return LuaJieshu